package com.pa.chong.pachong.tool.utils;

import com.google.gson.Gson;
import com.pa.chong.pachong.tool.dao.PhotoTypeDao;
import com.pa.chong.pachong.tool.dao.UserPhotoDao;
import com.pa.chong.pachong.tool.entity.PhotoType;
import com.pa.chong.pachong.tool.entity.UserPhoto;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.util.UUID;

@Component
public class QiNiuInput {

    @Autowired
    private UserPhotoDao userPhotoDao ;

    @Autowired
    private PhotoTypeDao photoTypeDao ;

    @Value("${myconfig.imgUrlTest}")
    private String testUrl;

    @Value("${myconfig.imgUrlHuaNan}")
    private String huaNanUrl;

    @Value("${myconfig.imgUrlHuaBei}")
    private String huaBeiUrl;

    @Value("${myconfig.imgUrlBeiMei}")
    private String beiMeiUrl;

    @Value("${myconfig.imgUrlDongNanYa}")
    private String dongNanYaUrl;

    @Value("${myconfig.usrBucket}")
    private String useBucket;

    public void upload(byte[] uploadBytes,String title,String imgType,String parentId,boolean flag){

        String imgHttpUrl = testUrl;
        Configuration cfg = new Configuration(Zone.autoZone());
        if(useBucket.equals("test")){
            cfg = new Configuration(Zone.zone0());
            imgHttpUrl = testUrl;
        }
        if(useBucket.equals("huanan")){
            cfg = new Configuration(Zone.zone2());
            imgHttpUrl = huaNanUrl;
        }
        if(useBucket.equals("huabei")){
            cfg = new Configuration(Zone.zone1());
            imgHttpUrl = huaBeiUrl;
        }
        if(useBucket.equals("beimei")){
            cfg = new Configuration(Zone.zoneNa0());
            imgHttpUrl = beiMeiUrl;
        }
        if(useBucket.equals("dongnanya")){
            cfg = new Configuration(Zone.zoneAs0());
            imgHttpUrl = dongNanYaUrl;
        }

        //构造一个带指定Zone对象的配置类

        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        String accessKey = "Tg0coh9a85Hg9p0UDr3oQ8rRCn5mHvsZUH7j34hZ";
        String secretKey = "RYn6gIxtAPDlAb3EQEacL4az6raezaTrCnDxTn34";
        String bucket = useBucket;
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = UUID.randomUUID().toString().replace("-","")+".jpg";
        try {
            ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
            try {
                Response response = uploadManager.put(byteInputStream,key,upToken,null, null);
                //解析上传成功的结果

                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                String imgurl = imgHttpUrl+putRet.key;

                if(flag){
                    PhotoType photoType = photoTypeDao.getPhotoTypeByParentId(parentId);
                    photoType.setImgUrl(imgurl);
                    photoType.setTitle(title);
                    photoTypeDao.save(photoType);
                }


                //100 靓妹  200 cosplay
                UserPhoto userPhoto  = new UserPhoto();
                userPhoto.setId(IDUtil.getLongId());
                userPhoto.setImgTitle(title);
                userPhoto.setImgType(imgType);
                userPhoto.setImgStatus("1");
                userPhoto.setImgUrl(imgurl);
                userPhoto.setUserName("admin");
                userPhoto.setParentId(parentId);

                userPhotoDao.save(userPhoto);
                System.out.println("保持成功");



            } catch (QiniuException ex) {
                ex.printStackTrace();
            }
        } catch (
                Exception ex) {
            ex.printStackTrace();
        }
    }
}
